package com.healthtop.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * * 椿熙堂信息研发部
 * Author: 道长 on 2016/10/12.
 */
public class StringUtils {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final String BUTTON_TEMPLATE = "<button id='${id}' class='${btn-class}'>${btn-text}</button>";

    private static final String BUTTON_EVENT_TEMPLATE = "<button id='${id}' onclick='${event}(${id})' class='${btn-class}'>${btn-text}</button>";

    public static String getBirthdayFromIdCardNumber(String idCardNumber){
        return idCardNumber.substring(6,14);
    }

    public static int getGenderFromIdCardNumber(String idCardNumber){
        int value = Integer.valueOf(idCardNumber.substring(16,17));
        System.out.println(value);
        return value & 1;
    }

    public static String build(){
        return UUID.randomUUID().toString().replaceAll("-","");
    }

    public static Date parseTimeString(String origin){
        try {
            return sdf.parse(origin);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String format(Date date){
        return sdf.format(date);
    }

    public static int parseAge(String idCardNumber){
        String date = idCardNumber.substring(6,10);
        int year = Calendar.getInstance().get(Calendar.YEAR);
        int originYear = Integer.parseInt(date);
        return year-originYear;
    }

    public static String buildButton(String id,String btnClass,String btnIcon,String text){
        return BUTTON_TEMPLATE.replace("${id}",id).replace("${btn-class}",btnClass).replace("${btn-icon}",btnIcon).replace("${btn-text}",text);
    }

    public static String buildButton(String id,String btnClass,String btnIcon,String text,String event){
        return BUTTON_EVENT_TEMPLATE.replace("${id}",id).replace("${btn-class}",btnClass).replace("${btn-icon}",btnIcon).replace("${btn-text}",text).replace("${event}",event);
    }

    public static void main(String[] args) {
        int age = parseAge("610524198710072817");
        System.out.println(age);
    }

}
